package com.lovestudying.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import java.util.Vector;

import com.lovestudying.dao.OrderDao;
import com.lovestudying.entity.Account;
import com.lovestudying.entity.Course;
import com.lovestudying.entity.Order;
import com.lovestudying.entity.Teacher;
import com.lovestudying.util.DBUtil;
import com.lovestudying.util.DBUtil_Pro;
import com.lovestudying.util.PageInfo;

/**
 * Order Dao 实现类
 * 
 * @author OldDeer
 *
 */
public class OrderDaoImpl implements OrderDao {

	@Override
	public Integer DeleteOrder(Map<String, Object> Where_Param) {
		// 创建sql语句
		StringBuffer sql = new StringBuffer("DELETE FROM `Order`");
		// Where参数列表
		Vector<Object> params = new Vector<>();
		// 根据Map中的情况构建 查询语句的条件
		if (Where_Param != null && Where_Param.size() != 0) {
			sql.append(" WHERE");
			int i = 0;
			for (Entry<String, Object> element : Where_Param.entrySet()) {
				sql.append(" " + element.getKey() + " = ?");
				params.add(element.getValue());
				i++;
				if (i < Where_Param.size()) {
					sql.append(" and");
				}
			}
		}

		return DBUtil_Pro.doUpdate(sql.toString(), params);
	}

	@Override
	public Integer InsertOrder(Map<String, Object> Value_Param) {
		// 创建sql语句
		StringBuffer sql = new StringBuffer("INSERT INTO  `Order` (");
		// values参数列表
		Vector<Object> params = new Vector<>();
		int i = 0;
		// 根据Map中的情况构建 Insert语句的条件
		for (Entry<String, Object> element : Value_Param.entrySet()) {
			sql.append(element.getKey());
			params.add(element.getValue());
			i++;
			if (i < Value_Param.size()) {
				sql.append(",");
			}
		}
		sql.append(") VALUES (");
		for (int j = 0; j < params.size(); j++) {
			sql.append("?");
			if (j < params.size() - 1) {
				sql.append(",");
			}
		}
		sql.append(")");
		return DBUtil_Pro.doUpdate(sql.toString(), params);
	}

	@Override
	public Integer UpdateOrder(Map<String, Object> Set_Param, Map<String, Object> Where_Param) {
		// 创建sql语句
		StringBuffer sql = new StringBuffer("UPDATE `Order` SET");
		// 参数列表
		Vector<Object> params = new Vector<>();

		int i = 0;
		// 根据Map中的情况构建 Insert语句的条件
		for (Entry<String, Object> element : Set_Param.entrySet()) {
			sql.append(" " + element.getKey() + " = ?");
			params.add(element.getValue());
			i++;
			if (i < Set_Param.size()) {
				sql.append(",");
			}
		}
		sql.append(" WHERE");
		int j = 0;
		for (Entry<String, Object> element : Where_Param.entrySet()) {
			sql.append(" " + element.getKey() + " = ?");
			params.add(element.getValue());
			j++;
			if (j < Where_Param.size()) {
				sql.append(" and");
			}
		}
		return DBUtil_Pro.doUpdate(sql.toString(), params);
	}

	@Override
	public PageInfo<Order> orderUniOrderDetail(Map<String, Object> Where_Param, Map<String, Object> OrdeBy_Param,
			Integer Lmt_pageSize, Integer Lmt_pageNum) {
		List<Order> list = new Vector<>();
		PageInfo<Order> OrderPage;
		ResultSet rs = null;
		// 创建sql语句
		StringBuffer sql = new StringBuffer();
		sql.append(
				"SELECT * FROM 	`Order` `order` INNER JOIN order_detail order_detail ON order_detail.order_id = `order`.order_id");
		// Where参数列表
		Vector<Object> params = new Vector<>();
		// 根据Map中的情况构建 查询语句的条件
		if (Where_Param != null && Where_Param.size() != 0) {
			sql.append(" WHERE");
			int i = 0;
			for (Entry<String, Object> element : Where_Param.entrySet()) {
				sql.append(" " + element.getKey() + " = ?");
				params.add(element.getValue());
				i++;
				if (i < Where_Param.size()) {
					sql.append(" and");
				}
			}
		}
		int total = 0;
		try {
			ResultSet last = DBUtil_Pro.doQuery(sql.toString(), params);
			last.last();
			total = last.getRow();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		// 添加OrderBy参数
		if (OrdeBy_Param != null && OrdeBy_Param.size() != 0) {
			sql.append(" ORDER BY");
			int i = 0;
			// 遍历Map
			for (Entry<String, Object> element : OrdeBy_Param.entrySet()) {
				sql.append(" " + element.getKey());
				// 做升降序设置，键的值是null则是默认的ASC，不为null则添加DESC
				if (element.getValue() != null) {
					sql.append(" DESC");
				}
				i++;
				if (i < OrdeBy_Param.size()) {
					sql.append(",");
				}
			}
		}
		// 添加Limit参数
		if (Lmt_pageNum != null && Lmt_pageSize != null) {
			sql.append(" LIMIT ?,?");
			params.add((Lmt_pageNum - 1) * Lmt_pageSize);
			params.add(Lmt_pageSize);
		}

		// 执行SQL语句
		rs = DBUtil_Pro.doQuery(sql.toString(), params);
		try {
			while (rs.next()) {
				Order order = new Order(rs.getString("order_id"),
						new Account(rs.getString("account_id"), null, null, null, null, null, null, null),
						rs.getDouble("order_cost"), rs.getInt("order_state"), rs.getString("order_create_date"), null);
				list.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		OrderPage = new PageInfo<>(list, Lmt_pageNum, Lmt_pageSize, total);

		return OrderPage;
	}

	@Override
	public PageInfo<Order> findOrderUniAccount(Map<String, Object> Where_Param,
			Map<String, Object> OrdeBy_Param, Integer Lmt_pageSize, Integer Lmt_pageNum) {
		List<Order> list = new Vector<>();
		PageInfo<Order> OrderPage;
		ResultSet rs = null;
		// 创建sql语句
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM `Order` `order` INNER JOIN Account account ON account.account_id = order.account_id");
		// Where参数列表
		Vector<Object> params = new Vector<>();
		// 根据Map中的情况构建 查询语句的条件
		if (Where_Param != null && Where_Param.size() != 0) {
			sql.append(" WHERE");
			int i = 0;
			for (Entry<String, Object> element : Where_Param.entrySet()) {
				sql.append(" " + element.getKey() + " = ?");
				params.add(element.getValue());
				i++;
				if (i < Where_Param.size()) {
					sql.append(" and");
				}
			}
		}
		int total = 0;
		try {
			ResultSet last = DBUtil_Pro.doQuery(sql.toString(), params);
			last.last();
			total = last.getRow();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		// 添加OrderBy参数
		if (OrdeBy_Param != null && OrdeBy_Param.size() != 0) {
			sql.append(" ORDER BY");
			int i = 0;
			// 遍历Map
			for (Entry<String, Object> element : OrdeBy_Param.entrySet()) {
				sql.append(" " + element.getKey());
				// 做升降序设置，键的值是null则是默认的ASC，不为null则添加DESC
				if (element.getValue() != null) {
					sql.append(" DESC");
				}
				i++;
				if (i < OrdeBy_Param.size()) {
					sql.append(",");
				}
			}
		}
		// 添加Limit参数
		if (Lmt_pageNum != null && Lmt_pageSize != null) {
			sql.append(" LIMIT ?,?");
			params.add((Lmt_pageNum - 1) * Lmt_pageSize);
			params.add(Lmt_pageSize);
		}

		// 执行SQL语句
		rs = DBUtil_Pro.doQuery(sql.toString(), params);
		try {
			while (rs.next()) {
				Order order = new Order(rs.getString("order_id"),
						new Account(rs.getString("account_id"), rs.getString("account_phone"),
								rs.getString("account_name"), rs.getString("account_email"),
								rs.getString("account_psw"), rs.getString("account_pic"), rs.getInt("account_state"),
								rs.getInt("account_Integral")),
						rs.getDouble("order_cost"), rs.getInt("order_state"), rs.getString("order_create_date"), null);
				list.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		OrderPage = new PageInfo<>(list, Lmt_pageNum, Lmt_pageSize, total);

		return OrderPage;
	}

	@Override
	public List<Order> findOrderByState(String accountId) {

		// 创建一个集合
		List<Order> list = new ArrayList<>();

		// 获取结果集
		ResultSet rs = DBUtil.find(
				"SELECT * FROM `Order` `order` INNER JOIN Account account ON account.account_id = order.account_id where (order.order_state = 0 or order.order_state = 1 or order.order_state = 2) and order.account_id = ?",
				accountId);

		try {
			while (rs.next()) {
				String orderId = rs.getString("order_id");
				String orderCreateDate = rs.getString("order_create_date");
				Double orderCost = rs.getDouble("order_cost");
				Integer orderState = rs.getInt("order_state");

				Order order = new Order();
				order.setOrder_id(orderId);
				order.setOrder_create_date(orderCreateDate);
				order.setOrder_cost(orderCost);
				order.setOrder_state(orderState);

				list.add(order);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public String addOrderListData(String orderId, List<String> courseIdList) {
		StringBuffer sql = new StringBuffer("INSERT Order_Detail (order_id,course_id) VALUES (?,?)");
		for (String string : courseIdList) {
			if (DBUtil.update(sql.toString(), orderId, string) != 1) {
				return "add " + string + " fail";
			}
		}
		return "success";
	}
}
